<?php

namespace app\admin\controller;

use cmf\controller\AdminBaseController;
use think\Db;


/**
 * Class VoteLogController
 * @package app\admin\controller
 * @adminMenuRoot(
 *     'name'   =>'投票记录',
 *     'action' =>'default',
 *     'parent' =>'',
 *     'display'=> true,
 *     'order'  => 30,
 *     'icon'   =>'th',
 *     'remark' =>'门户管理'
 * )
 */
class VoteLogController extends AdminBaseController
{
    /**
     * 投票管理
     * @adminMenu(
     *     'name'   => '投票管理',
     *     'parent' => 'default',
     *     'display'=> true,
     *     'hasView'=> true,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '投票管理',
     *     'param'  => ''
     * )
     */
    public function index(){
        $param=$this->request->param();
        $where = [];

        if(!empty($param['department_id'])){
            $where['department_id']=$param['department_id'];
        }

        $startTime = empty($param['start_time']) ? 0 : strtotime($param['start_time']);
        $endTime   = empty($param['end_time']) ? 0 : strtotime($param['end_time']);
        if (!empty($startTime) && !empty($endTime)) {
            $where['a.create_time'] = [['>= time', $startTime], ['<= time', $endTime]];
        } else {
            if (!empty($startTime)) {
                $where['a.create_time'] = ['>= time', $startTime];
            }
            if (!empty($endTime)) {
                $where['a.create_time'] = ['<= time', $endTime];
            }
        }

        $keyword = empty($param['keyword']) ? '' : $param['keyword'];
        if (!empty($keyword)) {
            $where['b.name|user_nickname|mobile|author'] = ['like', "%$keyword%"];
        }

        if (!empty($param['eat_date'])) {
            $where['eat_date'] = $param['eat_date'];
        }
        
        $result=Db::name('vote_log')->alias('a')
            ->join('__COOKBOOK__ b','a.cookbook_id=b.id','LEFT')
            ->join('__USER__ c','a.user_id=c.id','LEFT')
            ->join('__DEPARTMENT__ d','c.department_id=d.id','LEFT')
            ->field('a.*,b.name,user_nickname,mobile,author,d.name department')
            ->where($where)
            ->order('a.create_time','desc')
            ->paginate(20);
        $result->appends($param);
        $this->assign('list', $result->items());
        $this->assign('page', $result->render());
        $this->assign('start_time', isset($param['start_time']) ? $param['start_time'] : '');
        $this->assign('end_time', isset($param['end_time']) ? $param['end_time'] : '');
        $this->assign('keyword', isset($param['keyword']) ? $param['keyword'] : '');
        $this->assign('eat_date', isset($param['eat_date']) ? $param['eat_date'] : '');
        $this->assign('department_id', isset($param['department_id']) ? $param['department_id'] : '');

        $departments=Db::name('department')->column('name','id');
        $this->assign('departments', $departments);
        return $this->fetch();
    }

    /**
     * 投票结果
     * @adminMenu(
     *     'name'   => '投票结果',
     *     'parent' => 'default',
     *     'display'=> true,
     *     'hasView'=> true,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '投票结果',
     *     'param'  => ''
     * )
     */
    public function outcome(){
        $param=$this->request->param();
        $where = [];

        if(!empty($param['department_id'])){
            $where['department_id']=$param['department_id'];
        }

        $startTime = empty($param['start_time']) ? 0 : strtotime($param['start_time']);
        $endTime   = empty($param['end_time']) ? 0 : strtotime($param['end_time']);
        if (!empty($startTime) && !empty($endTime)) {
            $where['a.create_time'] = [['>= time', $startTime], ['<= time', $endTime]];
        } else {
            if (!empty($startTime)) {
                $where['a.create_time'] = ['>= time', $startTime];
            }
            if (!empty($endTime)) {
                $where['a.create_time'] = ['<= time', $endTime];
            }
        }

        $keyword = empty($param['keyword']) ? '' : $param['keyword'];
        if (!empty($keyword)) {
            $where['b.name|user_nickname|mobile|author'] = ['like', "%$keyword%"];
        }

        if (empty($param['eat_date'])) {
            $param['eat_date']=date('Y-m-d',strtotime('+1 day'));
        }
        $where['eat_date'] = $param['eat_date'];

        $result=Db::name('vote_log')->alias('a')
            ->join('__COOKBOOK__ b','a.cookbook_id=b.id','LEFT')
            ->join('__USER__ c','a.user_id=c.id','LEFT')
            ->join('__DEPARTMENT__ d','c.department_id=d.id','LEFT')
            ->field('sum(a.id) vote_num,eat_date,b.name,author,d.name department')
            ->group('cookbook_id')
            ->where($where)
            ->order('vote_num','desc')
            ->paginate(20);

        $result->appends($param);
        $this->assign('list', $result->items());
        $this->assign('page', $result->render());
        $this->assign('start_time', isset($param['start_time']) ? $param['start_time'] : '');
        $this->assign('end_time', isset($param['end_time']) ? $param['end_time'] : '');
        $this->assign('keyword', isset($param['keyword']) ? $param['keyword'] : '');
        $this->assign('eat_date', isset($param['eat_date']) ? $param['eat_date'] : '');
        $this->assign('department_id', isset($param['department_id']) ? $param['department_id'] : '');

        $departments=Db::name('department')->column('name','id');
        $this->assign('departments', $departments);
        return $this->fetch();
    }

}